package cn.xgj.sfzy.controller;

import cn.xgj.sfzy.entity.ArticleType;
import cn.xgj.sfzy.service.ArticleTypeService;
import cn.xgj.sfzy.util.Result;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @ClassName ArticleTypeController
 * @Description TODO
 * @Author 李政阳
 * @Date 2024年10月23日 19:50
 * @Version 1.0
 */
@RestController
@RequestMapping("/articleType")
public class ArticleTypeController {

    @Autowired
    private ArticleTypeService service;

    @PostMapping("/add")
    public Result add(@RequestBody ArticleType articleType){
        int add = service.add(articleType);
        if (add==0){
            return Result.error("添加失败");
        }else {
            return Result.success("添加成功");
        }
    }
    @DeleteMapping("/del/{articleTypeId}")
    public Result delete(@PathVariable(value = "articleTypeId") Integer id){
        int delete = service.delete(id);
        if (delete==0){
            return Result.error("删除失败");
        }else if (delete==-1){
            return Result.error("该类型相关数据未删除，请删除后再试");
        } else {
            return Result.success("删除成功");
        }
    }
    @PutMapping("/update")
    public Result update(@RequestBody ArticleType articleType){
        int update = service.update(articleType);
        if (update==0){
            return Result.error("修改失败");
        }else {
            return Result.success("修改成功");
        }
    }
    @GetMapping
    public Result findAll(ArticleType articleType){
        return Result.success(service.findAll(articleType));
    }
    @GetMapping("/findByPage")
    public Result findByPage(
            @RequestParam(required = false,defaultValue = "1") Integer pageNum,
            @RequestParam(required = false,defaultValue = "2") Integer pageSize,
            ArticleType articleType){
        PageInfo<ArticleType> info = service.findByPage(pageNum, pageSize, articleType);

            return Result.success(info);

    }






}
